package chapter19;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/25 10:01
 */
public class ThreadLocalDateFormat {
    private static ThreadLocal<DateFormat> formatThreadLocal = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static String dateToString(Date date) {
        return formatThreadLocal.get().format(date);
    }

    public static Date stringToDate(String date) throws ParseException {
        return formatThreadLocal.get().parse(date);
    }
}
